package mvp.com.bbc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import mvp.com.bbc.MainActivity;
import mvp.com.bbc.R;
import mvp.com.bbc.appContact.AppContact;
import mvp.com.bbc.base.AppActivity;
import mvp.com.bbc.base.BaseFragment;
import mvp.com.bbc.bean.LoginBean;
import mvp.com.bbc.di.component.DaggerLoginComponent;
import mvp.com.bbc.di.modules.LoginModules;
import mvp.com.bbc.ui.fragment.HomeDetailFragment;
import mvp.com.bbc.ui.fragment.LoginFragment;
import mvp.com.bbc.ui.presenter.LoginPresenter;
import mvp.com.bbc.utils.ShareP;

/**
 * Created by Administrator on 2017/9/29.
 */

public class LoginActivity extends AppCompatActivity implements AppContact.LoginView {

    private EditText mInputU, mInputP;
    private Button mBtnLogin;
    private LoginPresenter mPresemter;
    private String mUserPhon;
    private String mUserPws;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mInputU = (EditText) findViewById(R.id.et_input_u);
        mInputP = (EditText) findViewById(R.id.et_input_p);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mPresemter = DaggerLoginComponent.builder()
                .loginModules(new LoginModules(this))
                .build()
                .getPresemter();
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPhon = mInputU.getText().toString().trim();
                mUserPws = mInputP.getText().toString().trim();
                if (mUserPhon.isEmpty() || mUserPws.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    //登录成功
                    mPresemter.loginPresenter(mUserPhon, mUserPws);
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        });
    }

    @Override
    public void login(final LoginBean loginBean) {
        //保存信息 并进行界面之间的转换
        ShareP.setParam(LoginActivity.this, "userid", loginBean.getAu().getUserid());
        ShareP.setParam(LoginActivity.this, "age", loginBean.getAu().getAge());
        ShareP.setParam(LoginActivity.this, "icon", loginBean.getAu().getIcon());
        ShareP.setParam(LoginActivity.this, "username", loginBean.getAu().getNicename());
        ShareP.setParam(LoginActivity.this, "phone", loginBean.getAu().getPhone());
        ShareP.setParam(LoginActivity.this, "rolename", loginBean.getAu().getRolename());
        ShareP.setParam(LoginActivity.this, "sex", loginBean.getAu().getSex());
        ShareP.setParam(LoginActivity.this, "password", loginBean.getAu().getPassword());
    }
}
